ifndef IPTABLES_SRC
IPTABLES_SRC ?= iptables-1.4.5
endif

APPS_INSTALL_DIR=$(TP_INSTALL_DIR)

ifeq ($(CONFIG_IPV6),y)
iptables-y = ip6tables
iptables-install=ip6tables_install
iptables-clean = ip6tables_clean
else
iptables-y = iptables
iptables-install = iptables_install
iptables-clean = iptables_clean
endif

.PHONY:all
all:$(iptables-y)

.PHONY: iptables_configure iptables iptables_install iptables_clean

iptables_configure:
ifeq ($(strip $(IPTABLES_SRC)), iptables-1.4.5)
	cd $(IPTABLES_SRC) && (if test -e Makefile ; then make distclean ; fi && \
		./configure --prefix=$(APPS_INSTALL_DIR) \
		 --libdir=$(APPS_INSTALL_DIR)/lib \
		--build=i386 --host=$(CROSSMAKE_HOST)  \
		--with-ksource=$(KERNELPATH) \
		--disable-devel --disable-libipq \
		LDFLAGS="-Wl,-rpath -Wl,$(LIBC_PATH)/lib" \
		$(if $(filter-out $(CONFIG_IPV6),y),--disable-ipv6,))
else ifeq ($(strip $(IPTABLES_SRC)), iptables-1.4.10)
	cd $(IPTABLES_SRC) && (if test -e Makefile ; then make distclean ; fi && \
		./configure --prefix=$(APPS_INSTALL_DIR) \
		 --libdir=$(APPS_INSTALL_DIR)/lib \
		--build=i386 --host=$(CROSSMAKE_HOST)  \
		--with-ksource=$(KERNELPATH) \
		--disable-devel --disable-libipq \
		LDFLAGS="-Wl,-rpath -Wl,$(LIBC_PATH)/lib" \
		$(if $(filter-out $(CONFIG_IPV6),y),--disable-ipv6,))
else
	@echo nothing to do.
endif	

iptables: iptables_configure
	cd $(IPTABLES_SRC) && \
	$(MAKE) && \
	$(MAKE) install && \
	./rm_ext.sh
	rm -rf $(APPS_INSTALL_DIR)/lib/libexec
	-mv $(APPS_INSTALL_DIR)/libexec $(APPS_INSTALL_DIR)/lib
	rm -rf $(APPS_INSTALL_DIR)/share
	rm -rf $(APPS_INSTALL_DIR)/man

iptables_install:
	@cd $(IPTABLES_SRC) && \
	$(MAKE) install && \
	./rm_ext.sh
	@rm -rf $(APPS_INSTALL_DIR)/lib/libexec
	@-mv $(APPS_INSTALL_DIR)/libexec $(APPS_INSTALL_DIR)/lib
	@rm -rf $(APPS_INSTALL_DIR)/share
	@rm -rf $(APPS_INSTALL_DIR)/man

iptables_clean:
	cd $(IPTABLES_SRC) && \
	if test -e Makefile ; then make distclean ; fi

.PHONY: ip6tables_configure ip6tables ip6tables_install ip6tables_clean

ip6tables_configure:
	[ -f $(IPTABLES_SRC)/Makefile ] || \
	( \
	cd $(IPTABLES_SRC) && \
	./configure --prefix=$(APPS_INSTALL_DIR) \
	--build=i386 --host=arm-uclibc-linux26  \
	--with-ksource=$(KERNELPATH) \
	--disable-devel --disable-libipq \
	)

ip6tables: ip6tables_configure
	cd $(IPTABLES_SRC) && $(MAKE)

ip6tables_install:
	@cd $(IPTABLES_SRC) && \
	$(MAKE) install && \
	./rm_ext.sh
	@rm -rf $(APPS_INSTALL_DIR)/lib/libexec
	@-mv $(APPS_INSTALL_DIR)/libexec $(APPS_INSTALL_DIR)/lib
	@rm -rf $(APPS_INSTALL_DIR)/share
	@rm -rf $(APPS_INSTALL_DIR)/man

ip6tables_clean:
	cd $(IPTABLES_SRC) && \
	if test -e Makefile ; then make distclean ; fi

.PHONY:install clean
install:$(iptables-install)
clean:$(iptables-clean)
